<?php

defined( 'IN_DICE' ) or die( 'Access Error!' );

/**
 * model 创建类
 */
class model {
  private static $model;
  
  /**
   * 用户操作数据库
   *
   * @return DataBase
   */
  public function db( $line = 'user', $uid = 0 ) {
    if( !in_array( $line, array_keys( all::$cfg['db'] ) ) ) {
      model::logs()->debug( $line, 'sys.txt' );
      func::showerr( 'System DB Ini Error!' );
    }
    $aLine = all::$cfg['db'][$line];
    $uid = uint( $uid );
    if( isset( $aLine[0] ) && is_array( $aLine[0] ) && $uid > 0 ) {
      $aLine = $aLine[ $uid % count( $aLine ) ];
    }
    $objkey = 'db'.$line;
    if( !isset( self::$model[$objkey] ) || !is_object( self::$model[$objkey] ) ) {
       include_once DIR_LIB . 'class.db.php';
       self::$model[$objkey] = new DataBase( $aLine );
    }
    return self::$model[$objkey];
  }
  
  /**
   * Memcache操作对象制作函数
   * 
   * @param string $line Memcache的线路与端口的key,必须先在cfg.inc中配置
   * @param int $uid 端口散列所用的UID,必须>0并且$line所对应的是一个数组才可以生效
   * @return mcache
   */
  public function mcache( $line, $uid = 0 ) {
    if( !in_array( $line, array_keys( all::$cfg['mcache'] ) ) ) {
      model::logs()->debug( $line, 'sys.txt' );
      func::showerr( 'System MCache Ini Error!' );
    }
    $aLine = all::$cfg['mcache'][$line];
    $uid = uint( $uid );
    if( is_array( $aLine[0] ) && $uid > 0 ) {
      $aLine = $aLine[ $uid % count( $aLine ) ];
    }
    $objkey = 'mcache'.$line;
    if( !isset( self::$model[$objkey] ) || !is_object( self::$model[$objkey] ) ) {
       include_once DIR_LIB . 'class.mcache.php';
       self::$model[$objkey] = new mcache( $aLine );
    }
    return self::$model[$objkey];
  }
  
  /**
   * Redis操作对象制作函数
   * 
   * @param string $line Redis的线路与端口的key,必须先在cfg.inc中配置
   * @param int $uid 端口散列所用的UID,必须>0并且$line所对应的是一个数组才可以生效
   * @return redis_local
   */
  public function mredis( $line, $uid = 0 ) {
    if( !in_array( $line, array_keys( all::$cfg['mredis'] ) ) ) {
      model::logs()->debug( $line, 'sys.txt' );
      func::showerr( 'System MR Ini Error!' );
    }
    $aLine = all::$cfg['mredis'][$line];
    $uid = uint( $uid );
    if( is_array( $aLine[0] ) && $uid > 0 ) {
      $aLine = $aLine[ $uid % count( $aLine ) ];
    }
    $objkey = 'mredis'.$line;
    if( !isset( self::$model[$objkey] ) || !is_object( self::$model[$objkey] ) ) {
       include_once DIR_LIB . 'class.mredis.php';
       self::$model[$objkey] = new redis_local( $aLine );
    }
    return self::$model[$objkey];
  }
  
  /**
   * Tyrant操作对象制作函数
   * 
   * @param string $line tt的线路与端口的key,必须先在cfg.inc中配置
   * @param int $uid 端口散列所用的UID,必须>0并且$line所对应的是一个数组才可以生效
   * @return tt
   */
  public function tt( $line, $uid = 0 ) {
    if( !in_array( $line, array_keys( all::$cfg['tt'] ) ) ) {
      model::logs()->debug( $line, 'sys.txt' );
      func::showerr( 'System TT Ini Error!' );
    }
    $aLine = all::$cfg['tt'][$line];
    $uid = uint( $uid );
    if( is_array( $aLine[0] ) && $uid > 0 ) {
      $aLine = $aLine[ $uid % count( $aLine ) ];
    }
    $objkey = 'tt'.$line;
    if( !isset( self::$model[$objkey] ) || !is_object( self::$model[$objkey] ) ) {
       include_once DIR_LIB . 'class.tt.php';
       self::$model[$objkey] = new tt( $aLine );
    }
    return self::$model[$objkey];
  }
  
  /**
   * 平台构造生成器,目前支持renren与manyou,fiveone
   *
   * @param string $site
   * @return renren
   */
  public static function platform( $site = '' ) {
    $site = strlen( $site ) ? $site : all::$cfg['pflag'];
    if( !in_array( $site, all::$cfg['openpf'] ) ) {
      return null;
    }
    if( ! is_object( self::$model[$site] ) ) {
      include_once DIR_MDL . "platform/$site.php";
      $siteclass = $site == 'manyou' || $site == 'fb' ? 'p'.$site : $site;
      self::$model[$site] = new $siteclass();
    }
    return self::$model[$site];
  }

  /**
   * 制造一个后台使用的分页类
   * @param string $file 当前脚本文件名
   * @param int $total 总条数
   * @param int $per 每页记录数
   * @return page
   */
  public static function pageadmin( $file = '', $total = 1000, $per = 20 ) {
    if( !isset( self::$model['pageadmin'.$per] ) || !self::$model['pageadmin'.$per] ) {
      self::$model['pageadmin'.$per] = new page( $file ? $file : 'admin.php', $total, $per );
    }
    return self::$model['pageadmin'.$per];
  }

  /**
   * 支付中心的支付与现金币操作模型
   * @return paypal
   */
  public static function paypf( $method = 'paypal' ) {
    include_once DIR_MDL . 'paypf/paypf.php';
    $key = 'paypf'.$method;
    if( !isset( self::$model[$key] ) || !self::$model[$key] ) {
      include_once DIR_MDL . "paypf/$method.php";
      self::$model[$key] = new $method( all::$cfg['paysecret'][1] );
    }
    return self::$model[$key];
  }

  /**
   * 支付中心客户端操作库
   * @return pclient
   */
  public static function pclient( $appid ) {
    if( !isset( self::$model['pclient'] ) || !self::$model['pclient'] ) {
      include_once DIR_LIB . 'class.pclient.php';
      self::$model['pclient'] = new pclient( $appid );
    }
    return self::$model['pclient'];
  }

  /**
   * 支付中心服务端操作库
   * @return pserver
   */
  public static function pserver() {
    if( !isset( self::$model['pserver'] ) || !self::$model['pserver'] ) {
      include_once DIR_LIB . 'class.pserver.php';
      self::$model['pserver'] = new pserver();
    }
    return self::$model['pserver'];
  }

  /**
   * 会员操作模型model
   *
   * @return ModelMember
   */
  public static function member() {
    if( !isset( self::$model['member'] ) || !self::$model['member'] ) {
      include_once DIR_MDL . 'member.php';
      self::$model['member'] = new ModelMember();
    }
    return self::$model['member'];
  }

  /**
   * 在线表操作模型model
   *
   * @return ModelOnline
   */
  public static function online() {
    if( !isset( self::$model['online'] ) || !self::$model['online'] ) {
      include_once DIR_MDL . 'online.php';
      self::$model['online'] = new ModelOnline();
    }
    return self::$model['online'];
  }

  /**
   * 在线表操作模型model
   *
   * @return ModelLogs
   */
  public static function logs() {
    if( !isset( self::$model['logs'] ) || !self::$model['logs'] ) {
      include_once DIR_MDL . 'logs.php';
      self::$model['logs'] = new ModelLogs();
    }
    return self::$model['logs'];
  }

  /**
   * 桌子表操作模型model
   *
   * @return ModelTable
   */
  public static function table() {
    if( !isset( self::$model['table'] ) || !self::$model['table'] ) {
      include_once DIR_MDL . 'table.php';
      self::$model['table'] = new ModelTable();
    }
    return self::$model['table'];
  }

  /**
   * 任务操作模型model
   *
   * @return ModelMission
   */
  public static function mission() {
    if( !isset( self::$model['mission'] ) || !self::$model['mission'] ) {
      include_once DIR_MDL . 'mission.php';
      self::$model['mission'] = new ModelMission();
    }
    return self::$model['mission'];
  }

  /**
   * 道具操作模型model
   *
   * @return ModelTool
   */
  public static function tool() {
    if( !isset( self::$model['tool'] ) || !self::$model['tool'] ) {
      include_once DIR_MDL . 'tool.php';
      self::$model['tool'] = new ModelTool();
    }
    return self::$model['tool'];
  }

  /**
   * server操作模型model
   *
   * @return ModelServer
   */
  public static function server() {
    if( !isset( self::$model['server'] ) || !self::$model['server'] ) {
      include_once DIR_MDL . 'server.php';
      self::$model['server'] = new ModelServer();
    }
    return self::$model['server'];
  }

  /**
   * 好友操作模型model
   *
   * @return ModelFriend
   */
  public static function friend() {
    if( !isset( self::$model['friend'] ) || !self::$model['friend'] ) {
      include_once DIR_MDL . 'friend.php';
      self::$model['friend'] = new ModelFriend();
    }
    return self::$model['friend'];
  }

  /**
   * 管理员操作模型model
   *
   * @return ModelAdmin
   */
  public static function admin() {
    if( !isset( self::$model['admin'] ) || !self::$model['admin'] ) {
      include_once DIR_MDL . 'admin.php';
      self::$model['admin'] = new ModelAdmin();
    }
    return self::$model['admin'];
  }

  /**
   * 反馈操作模型
   *
   * @return ModelFeed
   */
  public static function feed() {
    if( !isset( self::$model['feed'] ) || !self::$model['feed'] ) {
      include_once DIR_MDL . 'feed.php';
      self::$model['feed'] = new ModelFeed();
    }
    return self::$model['feed'];
  }

  /**
   * app的支付与现金币操作模型
   * @return ModelApppay
   */
  public static function apppay() {
    if( !isset( self::$model['apppay'] ) || !self::$model['apppay'] ) {
      include_once DIR_MDL . 'apppay.php';
      self::$model['apppay'] = new ModelApppay();
    }
    return self::$model['apppay'];
  }

  /**
   * 支付中心的支付与现金币操作模型
   * @return ModelPay
   */
  public static function pay() {
    if( !isset( self::$model['pay'] ) || !self::$model['pay'] ) {
      include_once DIR_MDL . 'pay.php';
      self::$model['pay'] = new ModelPay();
    }
    return self::$model['pay'];
  }

  /**
   * 支付中心的支付与现金币操作模型
   * @return ModelGift
   */
  public static function gift() {
    if( !isset( self::$model['gift'] ) || !self::$model['gift'] ) {
      include_once DIR_MDL . 'gift.php';
      self::$model['gift'] = new ModelGift();
    }
    return self::$model['gift'];
  }

  /**
   * 统计操作
   * @return ModelCount
   */
  public static function count() {
    if( !isset( self::$model['count'] ) || !self::$model['count'] ) {
      include_once DIR_MDL . 'count.php';
      self::$model['count'] = new ModelCount();
    }
    return self::$model['count'];
  }

  
}